// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "Applications"

AndroidItem {
    id: _root
//    width: 720 * viewScale
//    height: 1134 * viewScale

    property variant imeSimejiPath: []              //simejiの波形パス
    property string phoneDialBackgroundPath: ""     //phoneの背景
    property variant jcbeamThemeList: []            //jcbeamのテーマリスト

    property bool imeViewRequest: appReady ? _application.item.imeViewRequest : false    // IMEをアプリ要求でONするか（起動時ON)
    property alias viewIme: _ime.visible                                                // IMEの表示状態を外から変更

    property string appPackageName: ""      // アプリのパッケージ名
    property string imePackageName: ""      // IMEのパッケージ名

    property bool appReady: _application.status === Loader.Ready    //アプリのロード状態

    onImeViewRequestChanged: {
        if(_ime.status === Loader.Ready){
            _ime.visible = imeViewRequest;
        }
    }

    //高さが変わった
    onHeightChanged: {
        if(_application.status === Loader.Ready){
            _application.setHeight();
        }
    }

    Item{
        anchors.fill: parent

        //アプリの表示
        Loader{
            id: _application
            width: _root.width
            height: _root.height - _ime.height
            onLoaded: {
                setHeight();
            }
            function setHeight(){
                if(item.imeViewMode === 0){
                    height = _root.height - _ime.height;
                }else{
                    height = _root.height;
                }
            }
        }
        //IMEの表示
        Loader{
            id: _ime
            anchors.bottom: parent.bottom
            width: _root.width
            height: (status === Loader.Ready && visible) ? item.height : 0
            visible: false
        }
    }

    Component.onCompleted: {
//        changeApp(appPackageName);
        changeIme(imePackageName);
    }

    onAppPackageNameChanged: {
        changeApp(appPackageName);
    }

    onImePackageNameChanged: {
        changeIme(imePackageName);
    }

    function changeApp(name){
        if(appPackageName.length > 0){
            _application.sourceComponent = getPackage(name);
        }
    }
    function changeIme(name){
        if(imePackageName.length > 0){
            _ime.sourceComponent = getPackage(name);
        }
    }

    //パッケージ名でコンポーネントを取得する
    function getPackage(name){
        var ret = undefined;
        if(installApplications === undefined){
        }else{
            for(var i=0; i<installApplications.length; i++){
                if(installApplications[i].name === name){
                    ret = installApplications[i].id;
                }
            }
        }
        return ret;
    }



    //インストール済みアプリ（ここに置くかは悩みどころ）
    property variant installApplications: [
        {"id": _comAdamrockerAndroidInputSimeji, "name": "com.adamrocker.android.input.simeji"}
        , {"id": _comGoogleAndroidGooglequicksearchbox, "name": "com.google.android.googlequicksearchbox"}
        , {"id": _comAndroidPhone, "name": "com.android.phone"}
        , {"id": _comJcromJcbeam, "name": "com.jcrom.jcbeam"}
                            ]

    //simeji
    Component{
        id: _comAdamrockerAndroidInputSimeji
        ComAdamrockerAndroidInputSimeji{
            viewScale: _root.viewScale
            isLock: _root.isLock
            isPortrait: _root.isPortrait
            visible: _root.imeViewRequest
            source: _root.imeSimejiPath
        }
    }

    //Google Search
    Component{
        id: _comGoogleAndroidGooglequicksearchbox
        ComGoogleAndroidGooglequicksearchbox{
            viewScale: _root.viewScale
            isLock: _root.isLock
            imeViewRequest: true
            isPortrait: _root.isPortrait
            onClicked: {
                _ime.visible = true;
            }
        }
    }

    //Phone
    Component{
        id: _comAndroidPhone
        ComAndroidPhone{
            viewScale: _root.viewScale
            isLock: _root.isLock
            isPortrait: _root.isPortrait
            dialBackgroundPath: _root.phoneDialBackgroundPath
        }
    }

    //jcbeam
    Component{
        id: _comJcromJcbeam
        ComJcromJcbeam{
            viewScale: _root.viewScale
            isLock: _root.isLock
            isPortrait: _root.isPortrait
            themeList: _root.jcbeamThemeList
        }
    }
}
